import java.util.HashMap;
import java.util.Map;

public class Solution2190 {
    public int mostFrequent(int[] nums, int key) {
        Map<Integer,Integer> map=new HashMap<>();
        for (int i=0;i<nums.length-1;i++){
            if (nums[i]==key){
                map.put(nums[i+1],map.getOrDefault(nums[i+1],0)+1);
            }
        }
        int min=Integer.MIN_VALUE;
        int ans=-1;
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            if (entry.getValue()>min){
                min=entry.getValue();
                ans= entry.getKey();
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution2190().mostFrequent(new int[]{1,100,200,1,100},1));
    }
}
